package metier.sess;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import metier.entities.Declaration;
import metier.entities.Offre;

@Stateless(name="Declaration")
public class DeclarationEJBImpl implements IDeclarationLocal{
	
	@PersistenceContext(unitName="PU_Cargaison")
	private EntityManager em;

	
	@Override
	public void addDeclaration(Declaration d) {
		System.out.println(d.getPrice());
		em.persist(d);
	}
	

	@Override
	public List<Declaration> getAllDeclaration() {
		Query req=em.createQuery("select d from Declaration d");
		return req.getResultList();
	}

	
	@Override
	public Declaration getDeclaration(Integer Id) {
		return em.find(Declaration.class, Id);
	}

	@Override
	public void updateDeclaration(Declaration d) {
		em.merge(d);
		
	}

	@Override
	public void deleteDeclaration(Integer Id) {
		em.remove(getDeclaration(Id));
	}

	@Override
	public List<Declaration> getAllDeclarationActive() {
		Query req=em.createQuery("select d from Declaration d where d.Statut=1");
		return req.getResultList();
	}
	
	
	@Override
	public List<Declaration> getDeclarationGagnante(Offre o)
	{
		Query r=em.createQuery("select min(d.price) from Declaration d where d.Statut=1 and  d.offre.idOffre="+o.getIdOffre()); 
		Query req=em.createQuery("select d from Declaration d where d.Statut=1 and d.price="+r.getResultList().get(0)+" and d.offre.idOffre="+o.getIdOffre());
		return req.getResultList();
	}
	
	@Override
	//retourne la liste des déclarations actifs type="Tra" pour transporteur et type="Exp" pour Expéditeur
	public List<Declaration> getAllDeclarationActivePrUser(Integer Id,String type,Integer statut) {
		Query req;
		String a="";
		String b="";
		
		if(statut==2)
		{
			b+="(";
			a+=" or  d.offre.statutOffre=-2)";
		}
		
		
		if(type.compareTo("Tra")==0)
		{
	    req=em.createQuery("select d from Declaration d where d.Statut="+statut+" and "+b+" d.offre.statutOffre=1 "+a+" and d.Transporteur.IdUtilisateur="+Id);
		}
		else
		{
	    req=em.createQuery("select d from Declaration d where d.Statut="+statut+" and  "+b+" d.offre.statutOffre=1  "+a+" and d.offre.Expediteur.IdUtilisateur="+Id);
		
		}
		return req.getResultList();
	}

//retourne la liste des declaration créée une fois un paiement est créé
	@Override
	public List<Declaration> getNewDeclarationsWithoutPrice(Integer IdUser,Offre o) {
		Query req=em.createQuery("select d from Declaration d where d.Statut=0 and d.price=0 and d.offre.idOffre="+o.getIdOffre()+" and d.Transporteur.IdUtilisateur="+IdUser+" ORDER BY d.DateCreation");
		System.out.println("select d from Declaration d where d.Statut=0 and d.price=0 and d.offre.idOffre="+o.getIdOffre()+" and d.Transporteur.IdUtilisateur="+IdUser+" ORDER BY d.DateCreation");
		return req.getResultList();
	}


	@Override
	public Double MinDeclaration(Integer IdOffre) {
		Query req=em.createQuery("select min(d.price) from Declaration d where d.Statut=1 and d.price!=0 and d.offre.idOffre="+IdOffre);
		return   (Double) req.getResultList().get(0);
		
	}
	
}
